package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.constants.Constants;
import com.slipper.exception.CommonException;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataFolderDao;
import com.slipper.system.codeless.bo.DataFolderBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class DataFolderService {
    @Resource
    private DataFolderDao dataFolderDao;
    
    public DataFolderBO queryBy(String id) {
        return dataFolderDao.queryBy(id);
    }
    
    public List<DataFolderBO> queryList(DataFolderBO bo) {
        return dataFolderDao.queryList(bo);
    }
    
    public PageInfo<DataFolderBO> queryPage(DataFolderBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataFolderDao.queryList(bo));
    }

    public long queryTotal(DataFolderBO bo) {
        PageHelper.startPage(0, 0);
        return new PageInfo<>(dataFolderDao.queryList(bo)).getTotal();
    }
    
    @WriteTransactional
    public int save(DataFolderBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return dataFolderDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return dataFolderDao.insert(bo);
        }
    }

    @WriteTransactional
    public void saveList(List<DataFolderBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataFolderDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataFolderDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataFolderDao.insert(item);
            }
        });
    }
    
    @WriteTransactional
    public int delete(String id) {
        DataFolderBO dataFolder = dataFolderDao.queryBy(id);
        if (dataFolder == null) return 0;
        if (Constants.YES.equals(dataFolder.getBuiltin())) throw new CommonException("内置文件夹，不允许删除！");
        if (dataFolderDao.queryUseCnt(id) > 0) throw new CommonException("文件夹已被引用，不允许删除！");

        return dataFolderDao.delete(id);
    }

    @WriteTransactional
    public int updateParentId(DataFolderBO bo) {
        return dataFolderDao.updateParentId(bo);
    }

    public boolean isParent(String id, String parentId) {
        if (Tool.isBlank(parentId) || Constants.ROOT_ID.equals(parentId)) {
            return false;
        }

        if (Tool.isBlank(id) || Constants.ROOT_ID.equals(id)) {
            return false;
        }

        DataFolderBO bo = dataFolderDao.queryBy(id);
        if (bo == null) {
            return false;
        } else if (bo.getParentId().equals(parentId)) {
            return true;
        }

        return isParent(bo.getParentId(), parentId);
    }
}
